﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;

namespace PPAP.Web.SearchReport
{
    public partial class Logfiles : PageBase
    {
        LogBO lbo = new LogBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                btnSubmit_Click(null, null);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = GetDataSet("  order by id desc ");
                AspNetPager2.RecordCount = ds.Tables[0].Rows.Count;
                dgList.DataSource = GetPageTable(ds.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
                dgList.DataBind();
            }
            catch (Exception ex)
            {
                AlertMessage(ex.Message.ToString());
            }
        }

        private DataSet GetDataSet(string sort)
        {
            string sql = " 1=1 ";
            if (txtPPARNo.Text.Trim() != "")
                sql += " and lower('PPAR-'||g.PPARNO||'.'||r.pparversion)  like lower('%" + txtPPARNo.Text.Trim() + "%') ";
            if (txtPartNo.Text.Trim() != "")
                sql += " and lower(g.partno) like lower('%" + txtPartNo.Text.Trim() + "%') ";
            if (txtUser.Text.Trim() != "")
                sql += " and lower(users) like lower('%" + txtUser.Text.Trim() + "%') ";
            if (txtStart.Text.Trim() != "")
                sql += " and trunc(logdate)>=to_date( '" + txtStart.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtEnd.Text.Trim() != "")
                sql += " and trunc(logdate)<=to_date( '" + txtEnd.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtOther.Visible == false && ddlAction.SelectedValue != "All")
                sql += " and lower(action) like  lower('%" + ddlAction.SelectedValue.Trim() + "%')  ";
            else if (txtOther.Visible)
                sql += " and lower(action) like  lower('%" + txtOther.Text.Trim() + "%')  ";
            DataSet ds = lbo.GetList(sql+sort);
            return ds;
        }

        public static DataTable GetPageTable(DataTable dt, int pageSize, int pageIndex)
        {
            if (dt != null)
            {
                DataTable dt1 = new DataTable();

                int pageEnd = pageIndex * pageSize;
                int pagebegin = (pageIndex - 1) * pageSize;


                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    dt1.Columns.Add(new DataColumn(dt.Columns[i].ColumnName));
                }

                for (int i = pagebegin; i < pageEnd; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        dt1.ImportRow(dt.Rows[i]);
                    }

                }
                return dt1;
            }
            else
                return null;
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            BindGridView();
        }

        /// <summary>
        /// Export
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = " 1=1 ";
                if (txtPPARNo.Text.Trim() != "")
                    sql += " and pparno='" + txtPPARNo.Text.Trim() + "' ";
                if (txtPartNo.Text.Trim() != "")
                    sql += " and partno='" + txtPartNo.Text.Trim() + "' ";
                if (txtUser.Text.Trim() != "")
                    sql += " and users='" + txtUser.Text.Trim() + "' ";
                if (txtStart.Text.Trim() != "")
                    sql += " and trunc(logdate)>=to_date( '" + txtStart.Text.Trim() + "', 'yyyy-mm-dd ') ";
                if (txtEnd.Text.Trim() != "")
                    sql += " and trunc(logdate)<=to_date( '" + txtEnd.Text.Trim() + "', 'yyyy-mm-dd ') ";
                if (txtOther.Visible == false && ddlAction.SelectedValue != "All")
                    sql += " and lower(action) like  lower('%" + ddlAction.SelectedValue.Trim() + "%')  ";
                else if (txtOther.Visible)
                    sql += " and lower(action) like  lower('%" + txtOther.Text.Trim() + "%')  ";
                DataSet ds = lbo.GetList(sql);
                ds.Tables[0].Columns.Remove("ID");
                CreateExcel(ds, "Log");
            }
            catch (Exception ex)
            {
                AlertMessage(ex.Message.ToString());
            }
        }

        protected void dgList_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();

            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.dgList.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.dgList.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.dgList.Attributes["SortExpression"] = sortExpression;
            this.dgList.Attributes["SortDirection"] = sortDirection;


            string pic = "";
            pic = sortDirection == "ASC" ? "<img src='../Images/icon/arrowup.gif' />" : "<img src='../Images/icon/arrowdown.gif' />";

            foreach (DataGridColumn item in dgList.Columns)
            {
                if (item.SortExpression == sortExpression)
                {
                    item.HeaderText = item.FooterText + " " + pic;
                }
                else
                    item.HeaderText = item.FooterText;
            }

            this.BindGridView();

        }

        private void BindGridView()
        {
            // 获取GridView排序数据列及排序方向
            string sortExpression = this.dgList.Attributes["SortExpression"];
            string sortDirection = this.dgList.Attributes["SortDirection"];
            if (sortExpression != null)
            {
                // 调用业务数据获取方法
                DataTable dtBind = GetDataSet(string.Format("  order by lower({0})  {1} ", sortExpression, sortDirection)).Tables[0];
                if (sortExpression == "logdate")
                {
                    dtBind = GetDataSet(string.Format("  order by {0}  {1} ", sortExpression, sortDirection)).Tables[0];
                }
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }
                // GridView绑定并显示数据
                this.dgList.DataSource = GetPageTable(dtBind, AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
                this.dgList.DataBind();
            }
            else
                btnSubmit_Click(null, null);
        }

        protected void ddlAction_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlAction.SelectedValue == "Other")
                txtOther.Visible = true;
            else
                txtOther.Visible = false;
        }

    }
}